﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLExpressDAL;
using System.Data.SqlClient;

public partial class Manageweb_Users_UserInfo : System.Web.UI.Page
{
    public string StrKey;
    DataTable dt;
    string sql;
    string userid;
    int num;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }

    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {
            this.userInfo.Visible = true;
            InitData();

        }
       

    }

    private void InitData()
    {
        userid = Session["userid"].ToString();
        sql = "select * from users where userid='" + userid + "'";
        dt = SqlHelper.FillDataSet(CommandType.Text, sql, null).Tables[0];
        TB_LoginID.Text = dt.Rows[0]["Userid"].ToString();
        TB_LoginName.Text = dt.Rows[0]["UserName"].ToString();
        DL_Sex.SelectedValue = Convert.ToByte(dt.Rows[0]["UserSex"]).ToString();
        TB_Tel.Text = dt.Rows[0]["UserPhone"].ToString();
        TB_Mobile.Text = dt.Rows[0]["UserMobile"].ToString();
        TB_Email.Text = dt.Rows[0]["UserEmail"].ToString();

        if(!string.IsNullOrEmpty(dt.Rows[0]["UserCountry"].ToString()))
        {
            TB_Country.Text = dt.Rows[0]["UserCountry"].ToString();
        }

       
        TB_City.Text = dt.Rows[0]["UserCity"].ToString();
        TB_CardCode.Text = dt.Rows[0]["UserCardCode"].ToString();
        TB_Address.Text = dt.Rows[0]["UserAddress"].ToString();
        TB_PostCode.Text = dt.Rows[0]["UserPostCode"].ToString();

    }
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30),
                new SqlParameter("@UserPhone",SqlDbType.VarChar,30),
                new SqlParameter("@UserMobile",SqlDbType.VarChar,11),
                new SqlParameter("@UserEmail",SqlDbType.VarChar,50),
                new SqlParameter("@UserCountry",SqlDbType.VarChar,50),
                new SqlParameter("@UserCity",SqlDbType.VarChar,30),
                new SqlParameter("@UserCardCode",SqlDbType.VarChar,18),
                new SqlParameter("@UserAddress",SqlDbType.VarChar,100),
                new SqlParameter("@UserPostCode",SqlDbType.VarChar,18)
            };
        paras[0].Value = Session["userid"];
        paras[1].Value = TB_Tel.Text;
        paras[2].Value = TB_Mobile.Text;
        paras[3].Value = TB_Email.Text;
        paras[4].Value = TB_Country.Text;
        paras[5].Value = TB_City.Text;
        paras[6].Value = TB_CardCode.Text;
        paras[7].Value = TB_Address.Text;
        paras[8].Value = TB_PostCode.Text;

        num = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "up_UpdateUsers", paras);
        if (num == 1)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('信息更新成功!');</script>");
        }
        else
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('更新失败!');</script>");

        }
    }
}
